<template>
	<view style="padding: 30px 15px">
		<view style="font-size: 16px; color: #1d1f20; font-weight: 400">微信号</view>
		<view style="margin-top: 15px; background-color: #fff; padding: 15px; border-radius: 10px">
			<u--input placeholder="请输入微信号" border="none" v-model="value"></u--input>
		</view>
		<!-- <view style="font-size: 16px; color: #1d1f20; font-weight: 400; margin-top: 30px">微信二维码</view>
		<view
			class="flex"
			style="
				background-color: #fff;
				padding: 80px 15px;
				border-radius: 10px;
				margin-top: 15px;
				justify-content: center;
			">
			<view>
				<u-upload
					:fileList="fileList1"
					@afterRead="afterRead"
					@delete="deletePic"
					name="1"
					multiple
					:maxCount="1"
					:previewFullImage="true"></u-upload>
			</view>
		</view>
		<view style="font-size: 12px; margin-top: 10px; color: #ff3331">
			请上传您的微信二维码图片 (尺寸1:1) ，方便您下级会员联系您
		</view> -->
		<view style="margin-top: 30px">
			<u-button @click="profile" shape="circle" type="primary" text="确定修改"></u-button>
		</view>
	</view>
</template>

<script>
	import config from '../../common/config/index.js';
	export default {
		data() {
			return {
				apiUrl: config.prodBase,
				imageurl: config.imgurl,
				value: '',
				fileList1: [],
				info: '',
			};
		},
		onLoad() {
			this.showUserInfo();
		},
		methods: {
			async profile() {
				let res = await this.$http.index.profile({
					wxcode: this.value,
					// wxqrcode: this.fileList1[0].fullurl,
				});
				if (res.code == 1) {
					this.$tips(res.msg);
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
						});
					}, 800);
				}
			},
			async showUserInfo() {
				let res = await this.$http.index.showUserInfo({});
				if (res.code == 1) {
					this.info = res.data;
					this.value = res.data.wxnum;
					if (this.info.wxqrcode != '') {
						let a = {
							fullurl: this.info.wxqrcode,
							url: this.imageurl + this.info.wxqrcode,
						};
						this.fileList1.push(a);
					}
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中',
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: 'success',
							message: '',
							url: result.data.fullurl,
							fullurl: result.data.url,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				// console.log(url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.apiUrl + 'common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync('token'),
						},
						formData: {
							user: 'test',
						},
						success: (res) => {
							console.log(res);
							setTimeout(() => {
								resolve(JSON.parse(res.data));
							}, 1000);
						},
					});
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	/deep/ .u-button--primary {
		background-color: #2447FF;
		border-color: #2447FF;
	}

	/deep/ .u-upload__button {
		width: 150px !important;
		height: 150px !important;
		margin: 0 !important;
	}

	/deep/ .u-upload__wrap__preview__image {
		width: 150px !important;
		height: 150px !important;
	}
</style>
<style>
	page {
		background-color: #f7f9fa;
	}
</style>
